package com.randb.easyhttp.utils;

import java.io.Closeable;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * User: RandBII
 * Date: 2020/5/9
 * Description:
 */
public class HttpUtil {

    public static final Charset UTF8 = StandardCharsets.UTF_8;

    public static String createUrlFromParams(String url, Map<String, String> params) {

        StringBuilder sb = new StringBuilder();
        try {
            sb.append(url);
            if (url.indexOf("&") > 0 || url.indexOf("?") > 0) {
                sb.append("&");
            } else {
                sb.append("?");
            }
            for (Map.Entry<String, String> entry : params.entrySet()) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            sb.deleteCharAt(sb.length() - 1);
            return sb.toString();
        } catch (Exception e) {
            HttpLog.e(e.getMessage());
        }
        return url;
    }

    public static void close(Closeable close) {
        if (close != null) {
            try {
                closeThrowException(close);
            } catch (IOException ignored) {

            }
        }
    }

    private static void closeThrowException(Closeable close) throws IOException {
        if (close != null) {
            close.close();
        }
    }



}
